package com.ruoyi.sale.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.FreightMethod;
import com.ruoyi.common.core.domain.ProductCates;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.mapper.FreightMethodMapper;
import com.ruoyi.sale.service.IFreightMethodService;

/**
 * 货运方式Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-17
 */
@Service
public class FreightMethodServiceImpl implements IFreightMethodService 
{
    @Autowired
    private FreightMethodMapper freightMethodMapper;

    /**
     * 查询货运方式
     * 
     * @param id 货运方式主键
     * @return 货运方式
     */
    @Override
    public FreightMethod selectFreightMethodById(Long id)
    {
        return freightMethodMapper.selectFreightMethodById(id);
    }

    /**
     * 查询货运方式列表
     * 
     * @param freightMethod 货运方式
     * @return 货运方式
     */
    @Override
    @DataScope(FreightMethodAlias = "fm")
    public List<FreightMethod> selectFreightMethodList(FreightMethod freightMethod)
    {
        return freightMethodMapper.selectFreightMethodList(freightMethod);
    }

    /**
     * 新增货运方式
     * 
     * @param freightMethod 货运方式
     * @return 结果
     */
    @Override
    public int insertFreightMethod(FreightMethod freightMethod)
    {
        freightMethod.setCreateTime(DateUtils.getNowDate());
        return freightMethodMapper.insertFreightMethod(freightMethod);
    }

    /**
     * 修改货运方式
     * 
     * @param freightMethod 货运方式
     * @return 结果
     */
    @Override
    public int updateFreightMethod(FreightMethod freightMethod)
    {
        freightMethod.setUpdateTime(DateUtils.getNowDate());
        return freightMethodMapper.updateFreightMethod(freightMethod);
    }

    /**
     * 批量删除货运方式
     * 
     * @param ids 需要删除的货运方式主键
     * @return 结果
     */
    @Override
    public int deleteFreightMethodByIds(Long[] ids)
    {
        return freightMethodMapper.deleteFreightMethodByIds(ids);
    }

    /**
     * 删除货运方式信息
     * 
     * @param id 货运方式主键
     * @return 结果
     */
    @Override
    public int deleteFreightMethodById(Long id)
    {
        return freightMethodMapper.deleteFreightMethodById(id);
    }

    @Override
    public List<TreeSelect> selectFreightMethodTreeList(FreightMethod freightMethod) {
        List<FreightMethod> freightMethodList = SpringUtils.getAopProxy(this).selectFreightMethodList(freightMethod);
        return buildFreightMethodTreeSelect(freightMethodList);
    }

    @Override
    public List<FreightMethod> buildFreightMethodTree(List<FreightMethod> freightMethodList) {
        List<FreightMethod> returnList = new ArrayList<>();
        List<Long> collect = freightMethodList.stream().map(FreightMethod::getId).collect(Collectors.toList());
        for (FreightMethod freightMethod : freightMethodList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(freightMethod.getParentId())){
                recursionFn(freightMethodList,freightMethod);
                returnList.add(freightMethod);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = freightMethodList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildFreightMethodTreeSelect(List<FreightMethod> freightMethodList) {
        List<FreightMethod> freightMethodTrees = buildFreightMethodTree(freightMethodList);
        return freightMethodTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param freightMethodList 子级
     * @param freightMethod 父级
     */
    private void recursionFn(List<FreightMethod> freightMethodList , FreightMethod freightMethod){
        // 得到子节点列表
        List<FreightMethod> childList = getChildList(freightMethodList, freightMethod);
        freightMethod.setChildrenFreightMethod(childList);
        for (FreightMethod newFreightMethod : childList)
        {
            if (hasChild(freightMethodList, newFreightMethod))
            {
                recursionFn(freightMethodList, newFreightMethod);
            }
        }
    }

    /**
     * 获得子节点
     * @param freightMethodList
     * @param freightMethod
     * @return
     */
    private List<FreightMethod> getChildList(List<FreightMethod> freightMethodList, FreightMethod freightMethod){
        List<FreightMethod> returnList = new ArrayList<>();
        Iterator<FreightMethod> iterator = freightMethodList.iterator();
        while (iterator.hasNext()){
            FreightMethod n = (FreightMethod) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == freightMethod.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<FreightMethod> freightMethodList, FreightMethod freightMethod)
    {
        return getChildList(freightMethodList, freightMethod).size() > 0;
    }
}
